<!--
 * @Descripttion: 碰撞结果
 * @version: 
 * @Author: 淏
 * @Date: 2023-11-24 13:13:39
 * @LastEditors: 淏
 * @LastEditTime: 2024-05-05 18:44:17
-->
<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i style="color: #00ffff" class="el-icon-receiving"></i>
          <span style="color: #00ffff">数据碰撞</span>
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-steps :active="active" simple style>
          <el-step
            title="步骤 1 选择碰撞左表"
            icon="el-icon-document"
          ></el-step>
          <el-step
            title="步骤 2 选择碰撞右表"
            icon="el-icon-document"
          ></el-step>
          <el-step
            title="步骤 3 数据碰撞入库"
            icon="el-icon-receiving"
          ></el-step>
        </el-steps>
      </div>
      <div style="text-align: center">
        <el-button
          class="button"
          @click="prev"
          v-waves
          plain
          style="width: 100px"
          v-if="active == 1 || active == 2"
        >
          上一步
        </el-button>
        <el-button
          class="button"
          @click="next"
          v-waves
          plain
          style="width: 100px"
          v-if="active == 0 || active == 1"
        >
          下一步
        </el-button>
        <el-button
          class="button"
          @click="insertData"
          v-waves
          plain
          style="width: 100px"
          v-if="active == 2"
        >
          开始碰撞
        </el-button>
      </div>
      <!-- 步骤 1 选择碰撞左表 -->
      <div v-if="active == 0" style="margin-top: 50px">
        <searchImDataLeft
          ref="leftSearchImData"
          :title="leftTitle"
          :key="0"
          @getColData="getLeftColData"
        ></searchImDataLeft>
      </div>
      <!--! !步骤 2 选择碰撞右表 -->
      <div v-if="active == 1" style="margin-top: 50px">
        <searchImDataRight
          ref="rightSearchImData"
          :title="rightTitle"
          :key="1"
          :tableCloR="tableCloR"
          :tableR="tableR"
          :tableQueryRulesR="tableQueryRulesR"
          @getColData="getRightColData"
        ></searchImDataRight>
      </div>
      <!-- 步骤 3 数据碰撞入库 -->
      <div v-if="active == 2" style="margin-top: 50px">
        <el-card class="cleatCard">
          <div slot="header" class="clearfix">
            <el-tag
              class="button"
              size="medium"
              style="font-size: 16px; margin-top: 0"
            >
              数据碰撞入库
            </el-tag>
            <span class="text"></span>
          </div>
          <el-form>
            <el-row>
              <div
                v-for="(itemColl, indexColl) in collisionColumnRules"
                :key="indexColl"
              >
                <el-col :span="24">
                  <el-form-item label="左表列" label-width="100px">
                    <span slot="label">
                      <span class="text">碰撞结果名称</span>
                    </span>
                    <el-input
                      size="mini"
                      style="width: 15vw"
                      v-model="collisionCnName"
                      placeholder="数据集名称"
                      class="handle-input mr10"
                    ></el-input>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="左表列" label-width="100px">
                    <span slot="label">
                      <span class="text">左表筛选列</span>
                    </span>
                    <el-select
                      v-model="itemColl.left"
                      placeholder="选择左表列"
                      class="handle-select mr10"
                      size="mini"
                      style="width: 25vw"
                      :popper-append-to-body="false"
                    >
                      <el-option
                        v-for="item in leftTableClo"
                        :key="item.id"
                        :label="item.columnCnName"
                        :value="item.columnEnName"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="7">
                  <el-form-item label="碰撞规则" label-width="100px">
                    <span slot="label">
                      <span class="text">碰撞规则</span>
                    </span>
                    <el-select
                      v-model="itemColl.rule"
                      placeholder="选择规则"
                      class="handle-select mr10"
                      size="mini"
                      :popper-append-to-body="false"
                    >
                      <el-option
                        v-for="item in ruleCollisionData"
                        :key="item.id"
                        :label="item.name"
                        :value="item.name"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="右表筛选列" label-width="100px">
                    <span slot="label">
                      <span class="text">右表筛选列</span>
                    </span>
                    <el-select
                      v-model="itemColl.right"
                      placeholder="选择左表列"
                      class="handle-select mr10"
                      size="mini"
                      style="width: 25vw"
                      :popper-append-to-body="false"
                    >
                      <el-option
                        v-for="item in rightTableClo"
                        :key="item.id"
                        :label="item.columnCnName"
                        :value="item.columnEnName"
                      ></el-option>
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="1">
                  <el-tooltip
                    class="item"
                    content="删除该条件"
                    placement="right-start"
                  >
                    <el-button
                      size="mini"
                      class="button"
                      style="margin-left: 20px; margin-top: 0"
                      type="danger"
                      icon="el-icon-delete"
                      circle
                      @click="removeCollision(itemColl)"
                    ></el-button>
                  </el-tooltip>
                </el-col>
              </div>
              <el-col :span="24">
                <el-tooltip content="添加碰撞条件" placement="left-start">
                  <el-button
                    size="mini"
                    class="button"
                    style="margin-left: 10px"
                    type="success"
                    icon="el-icon-circle-plus-outline"
                    plain
                    @click="addRules(itemColl)"
                  >
                    添加条件
                  </el-button>
                </el-tooltip>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script>
  import selectImport from '@/components/public/selectImport.vue'
  import selectAllRule from '@/components/select/selectAllRule.vue'
  import searchImDataLeft from './components/searchImDataLeft.vue'
  import searchImDataRight from './components/searchImDataRight.vue'
  import { query_rule, collision_import } from '@/api/index.js'
  export default {
    // 组件名称
    name: 'collisionResult',
    // 组件参数 接收来自父组件的数据
    props: {},
    // 局部注册的组件
    components: {
      selectImport,
      selectAllRule,
      searchImDataLeft,
      searchImDataRight,
    },
    // 组件状态值
    data() {
      return {
        leftTitle: '左表数据筛选',
        rightTitle: '右表数据筛选',
        active: 0,
        ruleType: 'query',
        ruleCollisionData: '',
        leftTableClo: [],
        rightTableClo: [],
        leftTable: {
          importId: '',
          tableColumns: [],
        },
        rightTable: {
          importId: '',
          tableColumns: [],
        },
        leftTableQueryRules: [
          {
            columnEnName: '',
            rule: [
              {
                ruleName: '',
                value1: '',
                isV1Filed: '',
                value2: '',
                isV2Filed: '',
                valueCount: '',
              },
            ],
          },
        ],
        rightTableQueryRules: [
          {
            columnEnName: '',
            rule: [
              {
                ruleName: '',
                value1: '',
                isV1Filed: '',
                value2: '',
                isV2Filed: '',
                valueCount: '',
              },
            ],
          },
        ],
        collisionCnName: '',
        collisionColumnRules: [
          {
            left: '',
            right: '',
            rule: '',
          },
        ],
      }
    },
    // 计算属性
    computed: {},
    // 侦听器
    watch: {},
    // 组件方法
    methods: {
      getRuleCollision() {
        let params = new URLSearchParams()
        params.append('type', 'collision')
        query_rule(params).then((res) => {
          this.ruleCollisionData = res.data
        })
      },
      /**
       * @description 组件方法
       * @author haoChen 15239820820@163.com
       * @export
       * @param {}
       * @return {}
       */
      //步骤条上一步按钮
      prev() {
        this.active -= 1
      },
      //步骤条下一步按钮
      next() {
        switch (this.active) {
          case 0:
            this.$refs['leftSearchImData'].saveData()
            this.active += 1
            /*  this.$refs['leftSearchImData'].clearData() */
            break
          case 1:
            this.$refs['rightSearchImData'].saveData()
            this.active += 1
            /* this.$refs['rightSearchImData'].clearData() */
            break
          default:
            break
        }
      },
      insertData() {
        if (
          this.collisionColumnRules.length == 0 ||
          this.collisionColumnRules[0].left == ''
        ) {
          this.$notify.error({
            title: '错误',
            message: '请正确选择碰撞数据！',
          })
        } else if (this.collisionColumnRules[0].rule == '') {
          this.$notify.error({
            title: '错误',
            message: '请正确选择碰撞规则！',
          })
        } else {
          let _this = this
          let leftTable = JSON.stringify(this.leftTable)
          let rightTable = JSON.stringify(this.rightTable)
          let leftTableQueryRules = JSON.stringify(this.leftTableQueryRules)
          let rightTableQueryRules = JSON.stringify(this.rightTableQueryRules)
          let collisionColumnRules = JSON.stringify(this.collisionColumnRules)
          let collisionCnName = this.collisionCnName
          var qs = require('qs')
          var data = qs.stringify({
            leftTable: leftTable,
            rightTable: rightTable,
            leftTableQueryRules: leftTableQueryRules,
            rightTableQueryRules: rightTableQueryRules,
            collisionColumnRules: collisionColumnRules,
            collisionCnName: collisionCnName,
          })
          this.$loading.show()
          collision_import(data)
            .then((res) => {
              console.log(res)
              if (res.code == 1) {
                this.$confirm(
                  res.message + '，请在碰撞结果页面查看数据！',
                  '提示',
                  {
                    showCancelButton: false,
                    confirmButtonText: '确定',
                    type: 'success',
                  }
                ).then(() => {
                  let imports = {
                    importData: {
                      createTime: '',
                      id: res.data,
                      name: '',
                      type: '',
                    },
                  }
                  this.$router.push({
                    path: '/resultData',
                    query: {
                      chooseImport: imports,
                    },
                  })
                })
              } else {
                this.$notify.error({
                  title: '错误',
                  message: '碰撞失败!!',
                })
              }
              this.$loading.close()
            })
            .catch((err) => {
              this.$loading.close()
            })
        }
      },
      /* 左表组件数据返回 */
      getLeftColData(table, tableClo, tableQueryRules) {
        if (table != undefined) {
          this.leftTable = table
        }
        if (tableClo != undefined) {
          this.leftTableClo = tableClo
        }
        if (tableQueryRules != undefined) {
          this.leftTableQueryRules = tableQueryRules
        }
      },
      /* 右表组件数据返回 */
      getRightColData(table, tableClo, tableQueryRules) {
        if (table != undefined) {
          this.rightTable = table
        }
        if (tableClo != undefined) {
          this.rightTableClo = tableClo
        }
        if (tableQueryRules != undefined) {
          this.rightTableQueryRules = tableQueryRules
        }
      },
      //新增条件
      addRules() {
        this.collisionColumnRules.push({
          name: '',
          value1: '',
          value2: '',
        })
      },
      //删除条件
      removeCollision(item) {
        var index = this.collisionColumnRules.indexOf(item)
        if (this.collisionColumnRules != 1) {
          if (index !== -1) {
            this.collisionColumnRules.splice(index, 1)
          }
        }
      },
    },
    created() {},
    mounted() {
      this.getRuleCollision()
    },
  }
</script>

<style scoped>
  .button {
    margin-top: 13px;
    background: rgba(0, 0, 255, 0.2);
    border-radius: 10px;
    border: 1px solid #295a85;
    color: #00ffff;
    text-shadow: 0 0 25px #00ffff;
    font-weight: 700;
  }
  .text {
    color: #00ffff;
    font-weight: 500;
  }
  .cleatCard {
    background: rgba(48, 89, 224, 0.3);
    border: 1px solid #295a85;
    overflow: auto; /* 自动开启滚动条 */
    height: calc(100vh - 280px); /* 设置最大高度 */
  }
</style>
<style lang="less" scoped>
  /deep/.el-steps--simple {
    background: #00ffff70;
  }
  /deep/.el-step__head.is-finish {
    color: #00ffff !important;
  }
  /deep/.el-step__title.is-finish {
    color: #00ffff !important;
    text-shadow: 0 0 25px #00ffff;
    font-weight: 700;
  }
  /deep/.el-step__title.is-process {
    color: #ffffff !important;
    text-shadow: 0 0 25px #ffffff;
  }
  /deep/.el-step__head.is-process {
    color: #ffffff !important;
    text-shadow: 0 0 25px #ffffff;
  }
  /deep/ .el-step__head.is-wait {
    color: #ffffff !important;
    text-shadow: 0 0 25px #ffffff;
  }
  /deep/ .el-step__title.is-wait {
    color: #ffffff !important;
    text-shadow: 0 0 25px #ffffff;
  }
  /deep/.el-card__header {
    border-bottom: 1px solid #00ffff;
  }
  /deep/.el-tag.el-tag--info {
    background: #00ffff70;
    border-color: #00ffff;
    color: #ffffff;
  }
</style>
